package com.yds.modewithprinceple.builder;

/**
 * @author YDS
 * @date 2021/5/7
 * @discribe builder工具类
 * <p>
 * 1、私有化构建方法，使其只能通过内部类Builder的build方法创建
 * 2、静态内部类作为桥梁，可以设置所有参数
 * 3、通过build的方法创建Person实例。
 * 4、由于Builder这个桥梁，使得构建时可以设置不同参数，把构建和参数解耦。
 * 5、创建实例后，参数不可再改。
 * 6、隐藏了构建过程，我们还可以在构建过程中增加一些处理，这些处理都是隐藏的。
 * <p>
 * 整体来说:
 * Builder模式核心就是隐藏构造方法，通过一个静态内部类封装构造。隐藏构建过程。
 */
@SuppressWarnings("all")
public class PersonUtil {
    private int age;
    private String name;
    private String address;
    private CallBack callBack;

    private PersonUtil(Builder builter) {
        this.age = builter.age;
        this.name = builter.name;
        this.address = builter.address;
        this.callBack = builter.callBack;
        callBack.doSomething(toString());
    }

    public interface CallBack {
        void doSomething(String msg);
    }

    @Override
    public String toString() {
        return "PersonUtil{" +
                "age=" + age +
                ", name='" + name + '\'' +
                ", address='" + address + '\'' +
                '}';
    }

    public static class Builder {
        private int age;
        private String name;
        private String address;
        private CallBack callBack;

        public Builder setAge(int age) {
            this.age = age;
            return this;
        }

        public Builder setName(String name) {
            this.name = name;
            return this;
        }

        public Builder setAddress(String address) {
            this.address = address;
            return this;
        }

        public Builder setCallBack(CallBack callback) {
            this.callBack = callback;
            return this;
        }

        public PersonUtil build() {
            return new PersonUtil(this);
        }

    }
}
